package top.infopub.mgc.util;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * SHA1(Secure Hash Algorithm)摘要算法
 * @author Awoke
 * @version 2019-6-23
 * @see SHA1
 * @since
 */
public final class SHA1 {

    private static final Logger log = LoggerFactory.getLogger(SHA1.class);

    /**
     * 转换成SHA1密文
     * @param plainText 明文
     * @return String 进行过SHA1算法后的密文
     * @see
     */
    public static String toSHA1(String plainText) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(plainText.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++ ) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();
        }
        catch (NoSuchAlgorithmException e) {
            log.error("NoSuchAlgorithmException:", e);
        }
        return "";
    }

}
